package XComponent.show
{
	import XComponent.XComponents;
	import XComponent.show.core.XUIComonent;
	
	import fl.controls.Button;
	import fl.controls.Label;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	
	public class XPopWnd extends XBitmapScale9Grid
	{
		private var m_btnClose : SimpleButton;
		
		private var m_offsetCloseBtn : Number;
		
		private var m_panTitle : Sprite;
		
		private var m_labTitle : Label;
		
		private var m_bitTitle : Bitmap;
		private var m_bidTitle : BitmapData;
		
		public function XPopWnd(skinId : String = "Frame_activeBG")
		{
			m_offsetCloseBtn = 27;
			
			setStyle(skinId);
		}
		
		public function setStyle(skinId : String):void
		{
			clearChildrean();
			
			var layerBitInfo : Object = XComponents.getImgData(skinId);
			
			var clas : Class = XComponents.getSkin(layerBitInfo["className"]);
			var bit : Bitmap = new Bitmap(new clas);
			
			super.Init(new Bitmap(new clas),layerBitInfo["scaleGridTop"], layerBitInfo["scaleGridBottom"], layerBitInfo["scaleGridLeft"], layerBitInfo["scaleGridRight"]);
			
			
			m_labTitle = new Label;
			m_bidTitle = new BitmapData(bit.width,28,true,0x00000000);
			m_bitTitle = new Bitmap(m_bidTitle);
			m_panTitle = new Sprite;
			
			var cla : Class =  XComponents.getSkin(XComponents.getBtnData("CommonBtnClose")["className"]) as Class;
			m_btnClose = (new cla) as SimpleButton;
			
			this.addChild(m_labTitle);
			m_panTitle.addChild(m_bitTitle);
			this.addChild(m_panTitle);
			this.addChild(m_btnClose);
			
			m_labTitle.mouseEnabled = false;
			m_labTitle.mouseChildren = false;
			
			m_labTitle.text = "PopWnd";
			m_labTitle.width = m_labTitle.textField.textWidth;
			m_labTitle.validateNow();
			m_labTitle.x = bit.width/2 - m_labTitle.width/2;
			
			setCloseBtnPosition();
		}
		
		public function get panTitle() : Sprite
		{
			return m_panTitle;
		}
		
		public function set title(value : String) : void
		{
			m_labTitle.text = value;
		}
		
		public function get closeBtn() : SimpleButton
		{
			return m_btnClose;
		}
		
		private function  setCloseBtnPosition():void
		{
			if(m_btnClose)
			{
				m_btnClose.x = this.width - m_offsetCloseBtn;
			}
		}
		
		override public function set width(w:Number):void
		{
			super.width = w;
			
			m_bitTitle.width = w;
			m_labTitle.x = w/2 - m_labTitle.width/2;;
		}
		
		public function clearChildrean() : void
		{
			for(var i:int = this.numChildren-1;i>=0;i--)
			{
				this.removeChildAt(i);
			}
		}
		
		public function update() : void
		{
			setCloseBtnPosition();
		}
	}
}